#pragma once
#include "Protocol.hpp"

enum
{
    DIVZERO = 1,
    MODZERO,
    OTHER_OPER
};

class Calculator
{
public:
    Calculator()
    {

    }

    Response CalculatorHelp(const Request& req)
    {
        Response res(0, 0);
        switch (req._op)
        {
        case '+':
            res._result = req._num1 + req._num2;
            break;
        case '-':
            res._result = req._num1 - req._num2;
            break;
        case '*':
            res._result = req._num1 * req._num2;
            break;
        case '/':
            if (req._num2 == 0)
            {
                res._code = DIVZERO;
            }
            else
            {
                res._result = req._num1 / req._num2;
            }
            break;
        case '%':
            if (req._num2 == 0)
            {
                res._code = MODZERO;
            }
            else
            {
                res._result = req._num1 % req._num2;
                break;
            }
            
        default:
            res._code = OTHER_OPER;
            break;
        }

        return res;
    }

    std::string Handler(std::string& package)
    {
        std::string content;
        bool r = Decode(package, &content);
        if (!r)
        {
            return "";
        }
        Request req;
        r = req.Deserialize(content);
        if (!r)
        {
            return "";
        }
        req.DebugPrint();
        content = "";
        Response res = CalculatorHelp(req);
        res.DebugPrint();
        res.Serialize(&content);
        content = Encode(content); // len\n正文\n

        return content;
    }

    ~Calculator()
    {

    }
};

